<template>
  <div class="login_container">
    <div class="login_box">
      <el-form :model="loginForm" ref="form" :rules="rules">
        <el-form-item prop="username">
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-zhanghao"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-mima"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button type="primary" @click="loginSubmit">登录</el-button>
          <el-button>重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
import { PostLoginApi } from '../request/api.js'
export default {
  name: 'login',
  data: () => {
    return {
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      rules: {
        username: [{ required: true, message: "请输入账号", trigger: "blur" }],
        password: [{ required: true, message: "请输入密码", trigger: "blur" }],
      },
    }
  },
  mounted () {

  },
  methods: {
    // 登录
    loginSubmit () {
      this.$refs.form.validate(valid => {
        if (!valid) return
        if (valid) {
          PostLoginApi(this.loginForm).then(res => {
            console.log(res)
            if (res.meta.status === 200) {
              // 存储token
              sessionStorage.setItem('token', res.data.token)
              this.$message.success(res.meta.msg);
              this.$router.push('/home')
            } else {
              this.$message.error(res.meta.msg);
            }

          })
        }
      })

    }
  }
}
</script>

<style lang="scss" scoped>
.login_container {
  // background-position: center;
  height: 100%;
  width: 100%;
  // background-size: cover;
  // position: fixed;
  // background: url("../assets/img/西施.jpg") no-repeat;
  background-image: url("../assets/img/西施.jpg");
  background-size: cover;
  background-position: center;
  position: relative;

  .login_box {
    width: 350px;
    height: 220px;
    background: rgb(124, 176, 235);
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate((-50%, -50%));
    border-radius: 3px;
    .btns {
      display: flex;
      justify-content: flex-end;
    }
  }
  form {
    padding: 50px;
    // margin-top: 94px;
    // margin-left: 88px;
  }
}
</style>>

